package com.hospital.config;

import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

/**
 * @description：自定义名称生成器
 * @author：zou
 * @date：2021/9/22 11:06
 * @versions：v1.0
 */

@Configuration
public class UniqueNameGenerator extends AnnotationBeanNameGenerator {

    @Override
    public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {

        //如果有设置value,则用value,没有则用全类名
        if (definition instanceof AnnotatedBeanDefinition) {

            String beanName = determineBeanNameFromAnnotation((AnnotatedBeanDefinition) definition);

            if (!StringUtils.hasText(beanName)) {
                //全限定类名
                beanName = definition.getBeanClassName();
            }

            return beanName;

        }

        //使用默认类名
        return buildDefaultBeanName(definition, registry);

    }

}
